package by.sjc.grsu.dao.repositiory.impl;

import by.sjc.grsu.dao.base.AbstractHibernateDao;
import by.sjc.grsu.dao.repositiory.AccountDao;
import by.sjc.grsu.model.Account;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class AccountRepository extends AbstractHibernateDao<Account, Integer> implements AccountDao {

    public static final Logger logger = Logger.getLogger(AccountRepository.class);

    @Override
    public Account loadAccountByEmail(String email) {
        @SuppressWarnings("JpaQlInspection")
        String hql = "from Account a where a.email = :email";

        //@SuppressWarnings("unchecked")
        return (Account) getSession().createQuery(hql)
                .setParameter("email", email).uniqueResult();
    }

    @Override
    public void registerAccount(Account account) {
        try {
            Session session = getSession();
            session.save(account);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
}
